﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace dzs.DAL
{
	/// <summary>
	/// 数据访问类:qualitysked1
	/// </summary>
	public partial class qualitysked1
	{
		public qualitysked1()
		{}
		#region  BasicMethod
        public bool setstate(string qidlist, string r)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update qualitysked1 set  qresult='" + r+"'");
            strSql.Append(" where qid in (" + qidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool setqList(string qidlist,int uid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update qualitysked1 set  uid="+uid);
            strSql.Append(" where qid in (" + qidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("qid", "qualitysked1"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int qid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from qualitysked1");
			strSql.Append(" where qid=@qid");
			SqlParameter[] parameters = {
					new SqlParameter("@qid", SqlDbType.Int,4)
			};
			parameters[0].Value = qid;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(dzs.Model.qualitysked1 model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into qualitysked1(");
			strSql.Append("projectunit,projectname,supplier,productname,productnum,productdw,engineer,onstreamtime,engineermode,productionstate,engineeropinion,plandate,uid,qresult)");
			strSql.Append(" values (");
			strSql.Append("@projectunit,@projectname,@supplier,@productname,@productnum,@productdw,@engineer,@onstreamtime,@engineermode,@productionstate,@engineeropinion,@plandate,@uid,@qresult)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@projectunit", SqlDbType.NVarChar,50),
					new SqlParameter("@projectname", SqlDbType.NVarChar,50),
					new SqlParameter("@supplier", SqlDbType.NVarChar,30),
					new SqlParameter("@productname", SqlDbType.NVarChar,30),
					new SqlParameter("@productnum", SqlDbType.Float,8),
					new SqlParameter("@productdw", SqlDbType.NVarChar,10),
					new SqlParameter("@engineer", SqlDbType.NVarChar,30),
					new SqlParameter("@onstreamtime", SqlDbType.DateTime),
					new SqlParameter("@engineermode", SqlDbType.NVarChar,20),
					new SqlParameter("@productionstate", SqlDbType.NVarChar,20),
					new SqlParameter("@engineeropinion", SqlDbType.NVarChar,50),
					new SqlParameter("@plandate", SqlDbType.DateTime),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@qresult", SqlDbType.NVarChar,10)};
			parameters[0].Value = model.projectunit;
			parameters[1].Value = model.projectname;
			parameters[2].Value = model.supplier;
			parameters[3].Value = model.productname;
			parameters[4].Value = model.productnum;
			parameters[5].Value = model.productdw;
			parameters[6].Value = model.engineer;
			parameters[7].Value = model.onstreamtime;
			parameters[8].Value = model.engineermode;
			parameters[9].Value = model.productionstate;
			parameters[10].Value = model.engineeropinion;
			parameters[11].Value = model.plandate;
			parameters[12].Value = model.uid;
			parameters[13].Value = model.qresult;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(dzs.Model.qualitysked1 model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update qualitysked1 set ");
			strSql.Append("projectunit=@projectunit,");
			strSql.Append("projectname=@projectname,");
			strSql.Append("supplier=@supplier,");
			strSql.Append("productname=@productname,");
			strSql.Append("productnum=@productnum,");
			strSql.Append("productdw=@productdw,");
			strSql.Append("engineer=@engineer,");
			strSql.Append("onstreamtime=@onstreamtime,");
			strSql.Append("engineermode=@engineermode,");
			strSql.Append("productionstate=@productionstate,");
			strSql.Append("engineeropinion=@engineeropinion,");
			strSql.Append("plandate=@plandate,");
			strSql.Append("uid=@uid,");
			strSql.Append("qresult=@qresult");
			strSql.Append(" where qid=@qid");
			SqlParameter[] parameters = {
					new SqlParameter("@projectunit", SqlDbType.NVarChar,50),
					new SqlParameter("@projectname", SqlDbType.NVarChar,50),
					new SqlParameter("@supplier", SqlDbType.NVarChar,30),
					new SqlParameter("@productname", SqlDbType.NVarChar,30),
					new SqlParameter("@productnum", SqlDbType.Float,8),
					new SqlParameter("@productdw", SqlDbType.NVarChar,10),
					new SqlParameter("@engineer", SqlDbType.NVarChar,30),
					new SqlParameter("@onstreamtime", SqlDbType.DateTime),
					new SqlParameter("@engineermode", SqlDbType.NVarChar,20),
					new SqlParameter("@productionstate", SqlDbType.NVarChar,20),
					new SqlParameter("@engineeropinion", SqlDbType.NVarChar,50),
					new SqlParameter("@plandate", SqlDbType.DateTime),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@qresult", SqlDbType.NVarChar,10),
					new SqlParameter("@qid", SqlDbType.Int,4)};
			parameters[0].Value = model.projectunit;
			parameters[1].Value = model.projectname;
			parameters[2].Value = model.supplier;
			parameters[3].Value = model.productname;
			parameters[4].Value = model.productnum;
			parameters[5].Value = model.productdw;
			parameters[6].Value = model.engineer;
			parameters[7].Value = model.onstreamtime;
			parameters[8].Value = model.engineermode;
			parameters[9].Value = model.productionstate;
			parameters[10].Value = model.engineeropinion;
			parameters[11].Value = model.plandate;
			parameters[12].Value = model.uid;
			parameters[13].Value = model.qresult;
			parameters[14].Value = model.qid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int qid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from qualitysked1 ");
			strSql.Append(" where qid=@qid");
			SqlParameter[] parameters = {
					new SqlParameter("@qid", SqlDbType.Int,4)
			};
			parameters[0].Value = qid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string qidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from qualitysked1 ");
			strSql.Append(" where qid in ("+qidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public dzs.Model.qualitysked1 GetModel(int qid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 qid,projectunit,projectname,supplier,productname,productnum,productdw,engineer,onstreamtime,engineermode,productionstate,engineeropinion,plandate,uid,qresult from qualitysked1 ");
			strSql.Append(" where qid=@qid");
			SqlParameter[] parameters = {
					new SqlParameter("@qid", SqlDbType.Int,4)
			};
			parameters[0].Value = qid;

			dzs.Model.qualitysked1 model=new dzs.Model.qualitysked1();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public dzs.Model.qualitysked1 DataRowToModel(DataRow row)
		{
			dzs.Model.qualitysked1 model=new dzs.Model.qualitysked1();
			if (row != null)
			{
				if(row["qid"]!=null && row["qid"].ToString()!="")
				{
					model.qid=int.Parse(row["qid"].ToString());
				}
				if(row["projectunit"]!=null)
				{
					model.projectunit=row["projectunit"].ToString();
				}
				if(row["projectname"]!=null)
				{
					model.projectname=row["projectname"].ToString();
				}
				if(row["supplier"]!=null)
				{
					model.supplier=row["supplier"].ToString();
				}
				if(row["productname"]!=null)
				{
					model.productname=row["productname"].ToString();
				}
				if(row["productnum"]!=null && row["productnum"].ToString()!="")
				{
					model.productnum=decimal.Parse(row["productnum"].ToString());
				}
				if(row["productdw"]!=null)
				{
					model.productdw=row["productdw"].ToString();
				}
				if(row["engineer"]!=null)
				{
					model.engineer=row["engineer"].ToString();
				}
				if(row["onstreamtime"]!=null && row["onstreamtime"].ToString()!="")
				{
					model.onstreamtime=DateTime.Parse(row["onstreamtime"].ToString());
				}
				if(row["engineermode"]!=null)
				{
					model.engineermode=row["engineermode"].ToString();
				}
				if(row["productionstate"]!=null)
				{
					model.productionstate=row["productionstate"].ToString();
				}
				if(row["engineeropinion"]!=null)
				{
					model.engineeropinion=row["engineeropinion"].ToString();
				}
				if(row["plandate"]!=null && row["plandate"].ToString()!="")
				{
					model.plandate=DateTime.Parse(row["plandate"].ToString());
				}
				if(row["uid"]!=null && row["uid"].ToString()!="")
				{
					model.uid=int.Parse(row["uid"].ToString());
				}
				if(row["qresult"]!=null)
				{
					model.qresult=row["qresult"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select qid,projectunit,projectname,supplier,productname,productnum,productdw,engineer,onstreamtime,engineermode,productionstate,engineeropinion,plandate,user_name,qresult ");
			strSql.Append(" FROM vqsked1 where 1=1 ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString()+" order by qid desc");
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" qid,projectunit,projectname,supplier,productname,productnum,productdw,engineer,onstreamtime,engineermode,productionstate,engineeropinion,plandate,uid,qresult ");
			strSql.Append(" FROM qualitysked1 ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM qualitysked1 ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.qid desc");
			}
			strSql.Append(")AS Row, T.*  from qualitysked1 T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "qualitysked1";
			parameters[1].Value = "qid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

